package com.mangosteen.interviewji.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mangosteen.interviewji.common.ErrorCode;
import com.mangosteen.interviewji.exception.BusinessException;
import com.mangosteen.interviewji.mapper.QuestionAssociateMapper;
import com.mangosteen.interviewji.model.entity.QuestionAssociate;
import com.mangosteen.interviewji.model.entity.QuestionFavour;
import com.mangosteen.interviewji.model.vo.QuestionVO;
import com.mangosteen.interviewji.service.QuestionFavourService;
import com.mangosteen.interviewji.mapper.QuestionFavourMapper;
import com.mangosteen.interviewji.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author ASUS
* @description 针对表【question_favour(题目收藏表)】的数据库操作Service实现
* @createDate 2025-11-10 16:46:32
*/
@Service
@Slf4j
public class QuestionFavourServiceImpl extends ServiceImpl<QuestionFavourMapper, QuestionFavour>
    implements QuestionFavourService{


    @Resource
    private QuestionAssociateMapper questionAssociateMapper;


    @Resource
    private QuestionFavourMapper questionFavourMapper;

    @Resource
    private QuestionService questionService;
    @Override
    @Transactional
    public Boolean submit(Long questionId, Long userId) {
        QueryWrapper<QuestionFavour> questionFavourQueryWrapper = new QueryWrapper<>();
        questionFavourQueryWrapper.eq("questionId",questionId);
        questionFavourQueryWrapper.eq("userId",userId);
        QuestionFavour questionFavour = this.getOne(questionFavourQueryWrapper);
        if (questionFavour!=null){
            //如果之前收藏过
            if (questionFavour.getIsCancel()==1){
                //收藏状态是1则是收藏操作

                doFavour(questionId,userId);
            }else {
                //收藏状态是0则是取消收藏操作
                cancel(questionId,userId);
            }
        }else {
            //如果之前没有收藏过则一定是收藏操作
            doFavour(questionId,userId);
        }
       return true;
    }

    @Override
    public Boolean getStatus(Long questionId, Long userId) {
        QueryWrapper<QuestionFavour> questionFavourQueryWrapper = new QueryWrapper<>();
        questionFavourQueryWrapper.eq("questionId",questionId);
        questionFavourQueryWrapper.eq("userId",userId);
        QuestionFavour questionFavour = this.getOne(questionFavourQueryWrapper);
        if (questionFavour!=null){
            return questionFavour.getIsCancel()==0;
        }
        return false;
    }

    @Override
    public Page<QuestionVO> getUserFavour(Long current, Long size, Long id, HttpServletRequest request) {
        QueryWrapper<QuestionFavour> questionFavourQueryWrapper = new QueryWrapper<>();
        questionFavourQueryWrapper.eq("userId",id);
        List<QuestionFavour> questionFavourList = this.list(questionFavourQueryWrapper);
        List<Long> questionIdList = questionFavourList.stream().map(QuestionFavour::getQuestionId).collect(Collectors.toList());
        return questionService.getQuestionVOPage(current,size,questionIdList,request);
    }

    private Boolean cancel(Long questionId, Long userId) {
        log.info("用户"+userId+"取消收藏");
        QueryWrapper<QuestionFavour> questionFavourQueryWrapper = new QueryWrapper<>();
        questionFavourQueryWrapper.eq("questionId",questionId);
        questionFavourQueryWrapper.eq("userId",userId);
        QuestionFavour questionFavour = this.getOne(questionFavourQueryWrapper);
        questionFavour.setIsCancel(1);
        questionFavourMapper.updateById(questionFavour);

        QueryWrapper<QuestionAssociate> questionAssociateQueryWrapper = new QueryWrapper<>();
        questionAssociateQueryWrapper.eq("questionId",questionId);
        QuestionAssociate questionAssociate = questionAssociateMapper.selectOne(questionAssociateQueryWrapper);
        if (questionAssociate==null){
           throw new BusinessException(ErrorCode.PARAMS_ERROR,"取消收藏失败");
        }else {
            questionAssociate.setFavourNum(questionAssociate.getFavourNum()-1);
            questionAssociateMapper.updateById(questionAssociate);
        }
        return true;
    }

    private Boolean doFavour(Long questionId, Long userId) {
        log.info("用户"+userId+"收藏");
        QueryWrapper<QuestionFavour> questionFavourQueryWrapper = new QueryWrapper<>();
        questionFavourQueryWrapper.eq("questionId",questionId);
        questionFavourQueryWrapper.eq("userId",userId);
        QuestionFavour questionFavour = this.getOne(questionFavourQueryWrapper);
        if (questionFavour!=null){
            questionFavour.setIsCancel(0);
            updateById(questionFavour);
        }else {
            questionFavour = new QuestionFavour();
            questionFavour.setQuestionId(questionId);
            questionFavour.setUserId(userId);
            questionFavour.setIsCancel(0);
            save(questionFavour);
        }
        QueryWrapper<QuestionAssociate> questionAssociateQueryWrapper = new QueryWrapper<>();
        questionAssociateQueryWrapper.eq("questionId",questionId);
        QuestionAssociate questionAssociate = questionAssociateMapper.selectOne(questionAssociateQueryWrapper);
        if (questionAssociate==null){
            questionAssociate = new QuestionAssociate();
            questionAssociate.setQuestionId(questionId);
            questionAssociate.setFavourNum(1);
            questionAssociateMapper.insert(questionAssociate);
        }else {
            questionAssociate.setFavourNum(questionAssociate.getFavourNum()+1);
            questionAssociateMapper.updateById(questionAssociate);
        }
        return true;
    }
}




